# 写入
file = open("D:/python_game/learning_1.txt", "w")
file.write("Sample file writing\n")
file.write("This is line 2\n")
file.close()

text_lines = [
    "Chapter 3\n",
    "Sample text data file\n"
    "This is the third line of text\n"]

file = open("D:/python_game/learning_1.txt", "w")
file.writelines(text_lines) # 每次重写都会刷新掉原来的

# 读取
file = open("D:/python_game/learning_1.txt", "r")
char = file.read(10)
print(char)

all_date = file.read()
print(all_date) # 以读取的不会再次读取,因此要再open一次


file = open("D:/python_game/learning_1.txt", "r")
one_line = file.readline()
print(one_line)

all_date = file.readlines()
print(all_date)

print("Lines:", len(all_date))
for line in all_date:
    print(line.strip())



